package com.company.ecis.controller;


import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.company.ecis.entity.BalanceInfo;
import com.company.ecis.entity.BankCard;
import com.company.ecis.entity.Trade;
import com.company.ecis.entity.Userinfo;
import com.company.ecis.service.BalanceInfoService;
import com.company.ecis.service.BankCardService;
import com.company.ecis.service.TradeService;
import com.company.ecis.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-08-04
 */
@RestController
@RequestMapping("/ecis")
public class BalanceInfoController {

    @Autowired
    BalanceInfoService balanceInfoService;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private TradeService tradeService;
    @Autowired
    private BankCardService bankCardService;


    @PostMapping("/balance-info")
    public String getBalance(HttpSession session){

        return balanceInfoService.getBalance(session);
    }

    @PostMapping("/rechargeBalance")
    public String addBalance(int userId, BigDecimal money, int payPassword, String cardNumber){
        JSONObject jsonObject = new JSONObject();
        // 判断支付密码是否相同
        // 使用MySQL查询 或 使用session查询
        Userinfo userinfo = userinfoService.selectOneByUserIdAndPayPassword(userId, payPassword);
        if (userinfo==null){
            jsonObject.set("code",405).set("msg","密码不正确");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 模拟银行端 给出银行卡扣款信息
        BankCard bankCard = bankCardService.getBankCardBycardNumber(cardNumber);
        System.out.println(bankCard);
        // 判断银行卡内余额是否满足扣款条件
        if (money.compareTo(bankCard.getBalance())>0){
            jsonObject.set("code",406).set("msg","银行卡内余额不足");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 扣除银行卡内余额
        bankCard.setBalance(bankCard.getBalance().subtract(money));
        bankCardService.updateBankCard(bankCard);
        // 插入流水表
        Trade trade = new Trade();
        trade.setPrice(money);
        trade.setPayerIdent(String.valueOf(userId));
        trade.setPayeeIdent(cardNumber);
        trade.setPayeeUsername(userinfo.getUsername());
        trade.setPayerUsername(userinfo.getUsername());
        trade.setGmtCreated(new Date());
        trade.setPayType("充值");
        trade.setCardId(userinfo.getCardId());
        tradeService.insertTrade(trade);
        // 增加余额 通过username更新余额
        BalanceInfo balanceInfo = new BalanceInfo();
        balanceInfo.setUsername(userinfo.getUsername());
        balanceInfo.setBalance(money);
        balanceInfoService.updateBalanceByUsername(balanceInfo);
        jsonObject.set("code",200).set("msg","充值成功");
        return JSONUtil.toJsonStr(jsonObject);
    }

    @PostMapping("/reflect")
    public String reduceBalance(int userId,BigDecimal money,int payPassword,String cardNumber){
        JSONObject jsonObject = new JSONObject();
        // 判断支付密码是否相同
        // 使用MySQL查询 或 使用session查询
        Userinfo userinfo = userinfoService.selectOneByUserIdAndPayPassword(userId, payPassword);
        if (userinfo==null){
            jsonObject.set("code",405).set("msg","密码不正确");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 判断账户余额是否满足体现要求
        BalanceInfo balanceInfo = balanceInfoService.getBalanceByUsername(userinfo.getUsername());
        // 判断银行卡内余额是否满足扣款条件
        if (money.compareTo(balanceInfo.getBalance())>0){
            jsonObject.set("code",406).set("msg","余额不足");
            return JSONUtil.toJsonStr(jsonObject);
        }
        // 扣除账户余额
        balanceInfo.setBalance(money.negate());
        balanceInfoService.updateBalanceByUsername(balanceInfo);
        // 插入流水表
        Trade trade = new Trade();
        trade.setPrice(money.negate());
        trade.setPayerIdent(cardNumber);
        trade.setPayeeIdent(String.valueOf(userId));
        trade.setPayeeUsername(userinfo.getUsername());
        trade.setPayerUsername(userinfo.getUsername());
        trade.setGmtCreated(new Date());
        trade.setPayType("提现");
        trade.setCardId(userinfo.getCardId());
        tradeService.insertTrade(trade);
        // 增加银行卡余额
        BankCard bankCard = bankCardService.getBankCardBycardNumber(cardNumber);
        bankCard.setBalance(bankCard.getBalance().add(money));
        bankCardService.updateBankCard(bankCard);
        jsonObject.set("code",200).set("msg","提现成功");
        return JSONUtil.toJsonStr(jsonObject);
    }

}

